﻿#region Copyright (c) 2011, Ares Alexander Nicolaou , All rights reserved.
//Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

//    Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//    Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Data.ConnectionUI;
using System.Xml.Linq;
using System.IO;

namespace System.DbSizeForecaster
{
    /// <summary>
    /// Provide a default implementation for the storage of DataConnection Dialog UI configuration.
    /// </summary>
    public class DataConnectionConfiguration : IDataConnectionConfiguration
    {
        private const string configFileName = @"DataConnection.xml";
        private string fullFilePath = null;
        private XDocument xDoc = null;

        // Available data sources:
        private IDictionary<string, DataSource> dataSources;

        // Available data providers: 
        private IDictionary<string, DataProvider> dataProviders;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="path">Configuration file path.</param>
        public DataConnectionConfiguration(string path)
        {
            if (!String.IsNullOrEmpty(path))
            {
                fullFilePath = Path.GetFullPath(Path.Combine(path, configFileName));
            }
            else
            {
                fullFilePath = Path.Combine(System.Environment.CurrentDirectory, configFileName);
            }
            if (!String.IsNullOrEmpty(fullFilePath) && File.Exists(fullFilePath))
            {
                xDoc = XDocument.Load(fullFilePath);
            }
            else
            {
                xDoc = new XDocument();
                xDoc.Add(new XElement("ConnectionDialog", new XElement("DataSourceSelection")));
            }

            this.RootElement = xDoc.Root;
        }

        public XElement RootElement { get; set; }

        public void LoadConfiguration(DataConnectionDialog dialog)
        {
            dialog.DataSources.Add(DataSource.SqlDataSource);

            //don't support these
            //dialog.DataSources.Add(DataSource.SqlFileDataSource);
            //dialog.DataSources.Add(DataSource.OracleDataSource);
            //dialog.DataSources.Add(DataSource.AccessDataSource);
            //dialog.DataSources.Add(DataSource.OdbcDataSource);
            //dialog.DataSources.Add(SqlCe.SqlCeDataSource);
            dialog.UnspecifiedDataSource.Providers.Add(DataProvider.SqlDataProvider);

            //don't support these
            //dialog.UnspecifiedDataSource.Providers.Add(DataProvider.OracleDataProvider);
            //dialog.UnspecifiedDataSource.Providers.Add(DataProvider.OleDBDataProvider);
            //dialog.UnspecifiedDataSource.Providers.Add(DataProvider.OdbcDataProvider);
            dialog.DataSources.Add(dialog.UnspecifiedDataSource);

            this.dataSources = new Dictionary<string, DataSource>();
            this.dataSources.Add(DataSource.SqlDataSource.Name, DataSource.SqlDataSource);
            //don't support these
            //this.dataSources.Add(DataSource.SqlFileDataSource.Name, DataSource.SqlFileDataSource);
            //this.dataSources.Add(DataSource.OracleDataSource.Name, DataSource.OracleDataSource);
            //this.dataSources.Add(DataSource.AccessDataSource.Name, DataSource.AccessDataSource);
            //this.dataSources.Add(DataSource.OdbcDataSource.Name, DataSource.OdbcDataSource);
            //this.dataSources.Add(SqlCe.SqlCeDataSource.Name, SqlCe.SqlCeDataSource);
            //this.dataSources.Add(dialog.UnspecifiedDataSource.DisplayName, dialog.UnspecifiedDataSource);

            this.dataProviders = new Dictionary<string, DataProvider>();
            //don't support these
            this.dataProviders.Add(DataProvider.SqlDataProvider.Name, DataProvider.SqlDataProvider);
            //this.dataProviders.Add(DataProvider.OracleDataProvider.Name, DataProvider.OracleDataProvider);
            //this.dataProviders.Add(DataProvider.OleDBDataProvider.Name, DataProvider.OleDBDataProvider);
            //this.dataProviders.Add(DataProvider.OdbcDataProvider.Name, DataProvider.OdbcDataProvider);
            //this.dataProviders.Add(SqlCe.SqlCeDataProvider.Name, SqlCe.SqlCeDataProvider);


            DataSource ds = null;
            string dsName = this.GetSelectedSource();
            if (!String.IsNullOrEmpty(dsName) && this.dataSources.TryGetValue(dsName, out ds))
            {
                dialog.SelectedDataSource = ds;
            }

            DataProvider dp = null;
            string dpName = this.GetSelectedProvider();
            if (!String.IsNullOrEmpty(dpName) && this.dataProviders.TryGetValue(dpName, out dp))
            {
                dialog.SelectedDataProvider = dp;
            }
        }

        public void SaveConfiguration(DataConnectionDialog dcd)
        {
            if (dcd.SaveSelection)
            {
                DataSource ds = dcd.SelectedDataSource;
                if (ds != null)
                {
                    if (ds == dcd.UnspecifiedDataSource)
                    {
                        this.SaveSelectedSource(ds.DisplayName);
                    }
                    else
                    {
                        this.SaveSelectedSource(ds.Name);
                    }
                }
                DataProvider dp = dcd.SelectedDataProvider;
                if (dp != null)
                {
                    this.SaveSelectedProvider(dp.Name);
                }

                xDoc.Save(fullFilePath);
            }
        }

        public string GetSelectedSource()
        {
            try
            {
                XElement xElem = this.RootElement.Element("DataSourceSelection");
                XElement sourceElem = xElem.Element("SelectedSource");
                if (sourceElem != null)
                {
                    return sourceElem.Value as string;
                }
            }
            catch
            {
                return null;
            }
            return null;
        }

        public string GetSelectedProvider()
        {
            try
            {
                XElement xElem = this.RootElement.Element("DataSourceSelection");
                XElement providerElem = xElem.Element("SelectedProvider");
                if (providerElem != null)
                {
                    return providerElem.Value as string;
                }
            }
            catch
            {
                return null;
            }
            return null;
        }

        public void SaveSelectedSource(string source)
        {
            if (!String.IsNullOrEmpty(source))
            {
                try
                {
                    XElement xElem = this.RootElement.Element("DataSourceSelection");
                    XElement sourceElem = xElem.Element("SelectedSource");
                    if (sourceElem != null)
                    {
                        sourceElem.Value = source;
                    }
                    else
                    {
                        xElem.Add(new XElement("SelectedSource", source));
                    }
                }
                catch
                {
                }
            }

        }

        public void SaveSelectedProvider(string provider)
        {
            if (!String.IsNullOrEmpty(provider))
            {
                try
                {
                    XElement xElem = this.RootElement.Element("DataSourceSelection");
                    XElement sourceElem = xElem.Element("SelectedProvider");
                    if (sourceElem != null)
                    {
                        sourceElem.Value = provider;
                    }
                    else
                    {
                        xElem.Add(new XElement("SelectedProvider", provider));
                    }
                }
                catch
                {
                }
            }
        }
    }
}
